﻿using RequirementAndBug.Common.Entities.RoleModels;
using RequirementAndBug.DAL;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace RequirementAndBug.BLL
{
    public class BaseService
    {
        private BaseDao dao = new BaseDao();
        public DbContext Db
        {
            get
            {               
               return dao.db;
            }
        }

        public IQueryable<T> LoadEntities<T>(Expression<Func<T, bool>> whereLambda) where T : class,new()
        {
            return dao.LoadEntities(whereLambda);
        }

        public T SaveEntity<T>(T entity) where T : class ,new()
        {
            return dao.SaveEntity<T>(entity);
        }

        public T EditEntity<T>(T entity) where T : class ,new()
        {
            return dao.EditEntity<T>(entity);
        }

        /// <summary>
        /// 获取全部按钮权限控制
        /// </summary>
        /// <returns></returns>
        public List<RoleButtonRight> GetRoleBtnFullList()
        {
            List<RoleButtonRight> list = new List<RoleButtonRight>();

            RoleButtonRight r = new RoleButtonRight();

            #region 管理员权限配置
            r.BtnName = "开发完毕";
            r.BtnTriggerScript = "DevelopOkFunc";
            r.RoleID = 1;
            r.ShowBtnStage = new List<int> { 0};
            list.Add(r);

            r = new RoleButtonRight();
            r.BtnName = "测试完毕";
            r.BtnTriggerScript = "TestOkFunc";
            r.RoleID = 1;
            r.ShowBtnStage = new List<int> { 1 };
            list.Add(r);

            r = new RoleButtonRight();
            r.BtnName = "重新开发";
            r.BtnTriggerScript = "DevelopAgainFunc";
            r.RoleID =1;
            r.ShowBtnStage = new List<int> { 1,2,3 };
            list.Add(r);

            r = new RoleButtonRight();
            r.BtnName = "验收结束";
            r.BtnTriggerScript = "AcceptOkFunc";
            r.RoleID = 1;
            r.ShowBtnStage = new List<int> { 2 };
            list.Add(r);

            r = new RoleButtonRight();
            r.BtnName = "删除";
            r.BtnTriggerScript = "DeleteOkFunc";
            r.RoleID = 1;
            r.ShowBtnStage = new List<int> { 0 ,1,2,3};
            list.Add(r);
            #endregion

            #region 测试组权限配置            

            r = new RoleButtonRight();
            r.BtnName = "测试完毕";
            r.BtnTriggerScript = "TestOkFunc";
            r.RoleID =2;
            r.ShowBtnStage = new List<int> { 1 };
            list.Add(r);

            r = new RoleButtonRight();
            r.BtnName = "重新开发";
            r.BtnTriggerScript = "DevelopAgainFunc";
            r.RoleID = 2;
            r.ShowBtnStage = new List<int> { 1, 2, 3 };
            list.Add(r);            
            #endregion

            #region 开发组权限配置
            r = new RoleButtonRight();
            r.BtnName = "开发完毕";
            r.BtnTriggerScript = "DevelopOkFunc";
            r.RoleID =3;
            r.ShowBtnStage = new List<int> { 0 };
            list.Add(r);            
            #endregion


            #region 验收组权限配置
            r = new RoleButtonRight();
            r.BtnName = "验收结束";
            r.BtnTriggerScript = "AcceptOkFunc";
            r.RoleID = 4;
            r.ShowBtnStage = new List<int> { 2 };
            list.Add(r);

            r = new RoleButtonRight();
            r.BtnName = "重新开发";
            r.BtnTriggerScript = "DevelopAgainFunc";
            r.RoleID = 4;
            r.ShowBtnStage = new List<int> { 1, 2, 3 };
            list.Add(r);

            r = new RoleButtonRight();
            r.BtnName = "删除";
            r.BtnTriggerScript = "DeleteOkFunc";
            r.RoleID =4;
            r.ShowBtnStage = new List<int> { 0, 1, 2, 3 };
            list.Add(r);
            #endregion

            return list;
        }
        /// <summary>
        /// 获取我的角色权限列表
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<RoleButtonRight> GetRoleBtnList(int roleID)
        {
            List<RoleButtonRight> fullList = GetRoleBtnFullList();
            List<RoleButtonRight> subList ;

            subList = fullList.FindAll(x => x.RoleID == roleID);
            if (subList == null)
            {
                return new List<RoleButtonRight>();
            }
            return subList;        
        }
    }
}
